﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAQFrameworks;
using System.Data.OleDb;
using System.Xml;
using PhysicalDataProvider;
using DTOFrameworks;
using FooFun.DataElements.Dtos;

namespace PasswordForm
{
    public partial class IndexInit : Form
    {
        public IndexInit()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OleDbDataAdapter ad = new OleDbDataAdapter();
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Desktop\office\database\FAQ.mdb";
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM t_Topics";
            ad.SelectCommand = cmd;

            DataTable dt = new DataTable();
            ad.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                string xml = row["Content"] as string;

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                MakeTopciIndex(doc);

            }

            MessageBox.Show("Done");
        }

        void MakeTopciIndex(XmlDocument xml)
        {
            XmlElement root = xml.DocumentElement;
            string forumId = root.Attributes["ForumId"].Value;
            string topicId = root.Attributes["TopicId"].Value;
            string status = root.Attributes["Status"].Value;
            string eventId = root.Attributes["EventId"].Value;

            Guid fId = DataProviderUtil.GetGuid(forumId);
            Guid tId = DataProviderUtil.GetGuid(topicId);
            int s = (int)Enum.Parse(typeof(TopicStatus), status);
            Guid eId = DataProviderUtil.GetGuid(eventId);

            EventManager eventManager = new EventManager();
            IDataElement e = eventManager.GetElement(new ObjectDataElement(eId));

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Desktop\office\database\FAQ.mdb";
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = @"SELECT * 
FROM t_TopicIndex WHERE TopicId='" +
                DataProviderUtil.GetGuidBase64(topicId) + "'";

            conn.Open();

            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                return;
            }
            dr.Close();

            string sql = @"INSERT INTO t_TopicIndex
(TopicId,Status,ForumId,UserId)
VALUES
('{0}',{1},'{2}','{3}')";

            sql = string.Format(sql,
                DataProviderUtil.GetGuidBase64(tId),
                s,
                DataProviderUtil.GetGuidBase64(fId),
                DataProviderUtil.GetGuidBase64(e["AccountId"].Value));
            cmd.CommandText = sql;

            cmd.ExecuteNonQuery();

            conn.Close();
        }
    }
}
